package org.batatasframework.support.database.metadatas;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.Assert;


public abstract class AbstractTableMetadata<Field extends FieldMetadata> implements TableMetadata{

	/** 表示元数据所在的资源存储器！ */
	private final DatabaseMetadataResource databaseMetadataRresource = DatabaseMetadataResource.getResource();

	protected final Map<String, Field> columns = new HashMap<>();
	
//	protected AbstractTableMetadata(DatabaseMetadataResource databaseMetadataRresource){
//		this.databaseMetadataRresource = databaseMetadataRresource;
//	}
	
	@Override
	public Collection<Field> getFieldMetadatas() {
		return columns.values();
	}
	
	public DatabaseMetadataResource getDatabaseMetadataResource() {
		return databaseMetadataRresource;
	}
	
	public void put(String name, Field field){
		columns.put(name, field);
	}
	

	/**
	 * 根据指标的编码查找指标，这个方法必须编码存在，否则抛出空指针异常
	 * @param key
	 * @return
	 */
	@Override
	public final FieldMetadata get(String key){
		FieldMetadata itemDefine = _get(key);
		Assert.notNull(itemDefine, "找不到指标项的定义：" + key);
		return itemDefine;
	}

	/**
	 * 只查询并返回指标，不对指标是否存在进行判断
	 * @param key
	 * @return
	 */
	@Override
	public final FieldMetadata getOrNull(String key){
		return _get(key);
	}
	
	protected FieldMetadata _get(String key){
		return columns.get(key);
	}
}
